<template>
    <!-- 底部 -->
    <div class="my-footer">
      <!-- 全选 -->
      <div class="custom-control custom-checkbox">
        <input type="checkbox" class="custom-control-input" id="footerCheck" v-model="isDan">
        <label class="custom-control-label" for="footerCheck">全选</label>
      </div>
      <!-- 合计 -->
      <div>
        <span>合计:</span>
        <span class="price">¥ {{price1}}</span>
      </div>
      <!-- 按钮 -->
      <button type="button" class="footer-btn btn btn-primary">结算 ( {{sum}} )</button>
    </div>
  </template>
  
  <script>
    
  export default {
    props:{
      //接收父组件传过来的值,限定为数组格式
      list:Array
    },
    data(){
      return {
        
      }
    },
    computed:{
      isDan:{
        set(a){
          //将全选状态传给父组件
         this.$emit('obj',a)
        },
        get(){
          //循环数组如果全部为true则影响全选状态
          return this.list.every(obj => obj.goods_state)
        }
      },
      sum(){
        //计算选中的数量
    return  this.list.reduce((su,obj) =>{
      //判断是否被选中
        if(obj.goods_state){
          //被选中的进行累加
          su += obj.goods_count
         }
         //将结果return出去
          return su
      },0)
      },
      price1(){
        //计算选中的数量
    return this.list.reduce((sum,obj)=>{
       //判断是否被选中
      if(obj.goods_state){
         //被选中的进行计算
        sum += obj.goods_price * obj.goods_count
      }
       //将结果return出去
      return sum       
    },0) 
      }
    }
  }
  </script>
  
  <style lang="less" scoped>
  .my-footer {
    position: fixed;
    z-index: 2;
    bottom: 0;
    width: 100%;
    height: 50px;
    border-top: 1px solid #ccc;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px;
    background: #fff;
  
    .price {
      color: red;
      font-weight: bold;
      font-size: 15px;
    }
    .footer-btn {
      min-width: 80px;
      height: 30px;
      line-height: 30px;
      border-radius: 25px;
      padding: 0;
    }
  }
  </style>